const Router = require('koa-router')
const requireDirectory = require('require-directory')

class InitManager {
  static initCore(app) {
    InitManager.app = app
    InitManager.initLoadRouters()
    InitManager.loadHttpException()
    InitManager.loadConfig()
  }

  static loadConfig(path='') {
    const configPath = path || process.cwd() + '/config/config.js'
    const config = require(configPath)
    global.config = config
  }

  static initLoadRouters(){
    const apiDirectory = `${process.cwd()}/app/api`
    requireDirectory(module, apiDirectory, {
        visit: whenLoadModule
    })
    
    function whenLoadModule(obj) {
        if(obj instanceof Router ){
            InitManager.app.use(obj.routes())  // 这里加载路由，注意加载项为 routes()
        }
    }
  }

  static loadHttpException(){
      const errors = require('./http-exception')
      global.errors = errors
  }
}

module.exports = InitManager
